from hep.aida import *

from java.awt import Color
from java.awt import Font
from java.lang import *
from java.util import *
from java.io import *

from jhplot.jadraw import *
from jhplot  import *

from fr.in2p3.ipnl.aidautilities import *
from fr.in2p3.ipnl.oflap.scint import *
from jscinttools..utilities.jhplotutilities import *

Locale.setDefault(Locale.ENGLISH)
System.getProperties().setProperty("hep.aida.BatchAnalysisFactory","hep.aida.ref.BatchAnalysisFactory")

showFigures = False
writeFigures = not showFigures

""" Define colors and fonts"""
figType = [".png", ".eps"]
colors = [Color.blue, Color.red, Color(0, 170, 0), Color.black, Color.yellow]
axisLabelFont = Font(Font.SERIF, Font.BOLD, 20)
titleFont = Font(Font.SERIF, Font.BOLD, 25)
legendFont = Font(Font.SERIF, Font.PLAIN, 20)
ticFont = Font(Font.SERIF, Font.PLAIN, 17)
statBoxFont = Font(Font.SERIF, Font.PLAIN, 15)

""" Define the Path and files names"""
aidaPathL = "/home/marc-antoine/Documents/BGO/QFin"
aidaFileL = "ScintillationAnalysis_BGO"
#aidaPathQ = "/home/marc-antoine/Documents/BGO/QFin/QueensData/queenstools"
#aidaFileQ = "ScintillationAnalysis_BGO_QFile"
outPath = "/home/marc-antoine/Dropbox/BGO"
afrL = AidaFileReader(aidaPathL, aidaFileL)
#afrQ = AidaFileReader(aidaPathQ, aidaFileQ)

temper = ["299","30","3"]
numComp = ["2","1","3"]
labPosY = [0.305, 0.6, 0.895]
funcSup = [3,900,900]

"""
###############################################################
# Plotting Part
###############################################################
"""
""" Create the plottable JHep objects from AIDA IObjects"""
avEvt = []
func = []
label = []
for i in range(len(temper)):
	avEvt.append(H1D(afrL.getHisto("/Average_Events/Average_Events_" + temper[i] + ".0K")))
	avEvt[i].title = temper[i] + " K"
	avEvt[i].style = "h"
	#avEvt[i].setSymbol(0)
	#avEvt[i].setSymbolSize(12)
	avEvt[i].color = colors[0]
	avEvt[i].penWidth = 2
	avEvt[i].errY = False
	avEvt[i].errX = False
	#avEvt[i].errColor = mainP1D.getColor()
	
	func.append(F1D(afrL.getFunc("/Average_Events/Average_Event_Func_" + temper[i] + ".0K_" + numComp[i] + "Comp"), -100, funcSup[i]))
	func[i].title = temper[i]+ " K"
	func[i].points = 10000
	func[i].color = colors[1]
	func[i].penWidth = 1
	
	label.append(JaText(temper[i]+ " K", 0.2, labPosY[i], "NDC"))
	label[i].font = legendFont
	label[i].color = colors[0]
	
	

""" Create the Plotter"""
mainPlotter = HPlotJa("Average Events", 600, 1000, 1, 4, True)
#mainPlotter.setGTitle("Average Events in BGO", titleFont)
mainPlotter.antiAlias = False
if showFigures:
	mainPlotter.visible(True)
else:
	mainPlotter.visible(False)
	
mainPlotter.cd(1,1)	
mainPlotter.setPad(1, 1, 0.125, 0.075, 0.75, 0.26)
mainPlotter.setRange(0, 2.7, 0.0001, 1)
mainPlotter.setLogScale(0, False)
mainPlotter.setLogScale(1, True)
mainPlotter.ticFont = ticFont
mainPlotter.showStatBox = False
mainPlotter.showKey(False)
mainPlotter.nameX = ""
mainPlotter.nameY = ""

mainPlotter.cd(1,2)	
mainPlotter.setPad(1, 2, 0.125, 0.37, 0.75, 0.26)
mainPlotter.setRange(0, 135, 0.001, 1)
mainPlotter.setLogScale(0, False)
mainPlotter.setLogScale(1, True)
mainPlotter.ticFont = ticFont
mainPlotter.showStatBox = False
mainPlotter.showKey(False)
mainPlotter.nameX = ""
mainPlotter.nameY = ""

mainPlotter.cd(1,3)	
mainPlotter.setPad(1, 3, 0.6, 0.69, 0.225, 0.125)
mainPlotter.setRange(0, 15, 0.1, 1)
mainPlotter.setLogScale(0, False)
mainPlotter.setLogScale(1, True)
mainPlotter.ticFont = ticFont
mainPlotter.showStatBox = False
mainPlotter.showKey(False)
mainPlotter.nameX = ""
mainPlotter.nameY = ""

mainPlotter.cd(1,4)	
mainPlotter.setPad(1, 4, 0.125, 0.665, 0.75, 0.26)
mainPlotter.setRange(0, 900, 0.01, 1)
mainPlotter.setLogScale(0, False)
mainPlotter.setLogScale(1, True)
mainPlotter.ticFont = ticFont
mainPlotter.showStatBox = False
mainPlotter.showKey(False)
mainPlotter.nameX = "Time (&micro;s)"
mainPlotter.nameY = "Amplitude (a.u)"



""" Plots all the JHep objects"""
mainPlotter.cd(1,1)
mainPlotter.draw(avEvt[0])
mainPlotter.draw(func[0])
mainPlotter.draw(label[0])
theAxes_11 = mainPlotter.getPad()
theAxes_11.setLabelSpace(0, 0.0)
theAxes_11.setLabelSpace(1, -0.010)
HPlotJaUtil.setTickLabelOneToPower(1, theAxes_11)

mainPlotter.cd(1,2)
mainPlotter.draw(avEvt[1])
mainPlotter.draw(func[1])
mainPlotter.draw(label[1])
theAxes_12 = mainPlotter.getPad()
theAxes_12.setLabelSpace(0, 0.0)
theAxes_12.setLabelSpace(1, -0.010)
HPlotJaUtil.setTickLabelOneToPower(1, theAxes_12)

mainPlotter.cd(1,3)
mainPlotter.draw(avEvt[2])
mainPlotter.draw(func[2])
mainPlotter.draw(label[2])
theAxes_13 = mainPlotter.getPad()
theAxes_13.setLabelSpace(0, 0.0)
theAxes_13.setLabelSpace(1, -0.010)
HPlotJaUtil.setTickLabelOneToPower(1, theAxes_13)

mainPlotter.cd(1,4)
mainPlotter.draw(avEvt[2])
mainPlotter.draw(func[2])
theAxes_14 = mainPlotter.getPad()
theAxes_14.setLabelSpace(0, 0.0)
theAxes_14.setLabelSpace(1, -0.010)
HPlotJaUtil.setTickLabelOneToPower(1, theAxes_14)

""" Modifies the JaAxes and the axes labels"""
labX = mainPlotter.getLabelX()[0][3]
labX.setFont(axisLabelFont)
HPlotJaUtil.setLabelLocationAtCenter(0, labX, mainPlotter, 0.025)
labY = mainPlotter.getLabelY()[0][3]
labY.setFont(axisLabelFont)
HPlotJaUtil.setLabelLocationAtCenter(1, labY, mainPlotter, 0.015)

globTitle = JaText()
globTitle.text = "Average Events in BGO"
globTitle.font = titleFont
mainPlotter.draw(globTitle)
HPlotJaUtil.setLabelLocationAtCenter(0, globTitle, mainPlotter, 0.955)


mainPlotter.update()

""" Saves or not the plot"""
if writeFigures:
	for i in range(len(figType)):
		outFileName = "AvEvtMulti" + figType[i]
		mainPlotter.export(outPath + File.separator + outFileName)
		print("Writting File : " + outPath + File.separator + outFileName)
		
mainPlotter.update()
print("That's all folks !")

